package ltd.hxya.novel.book.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import ltd.hxya.novel.book.entity.BookRank;
import ltd.hxya.novel.book.mapper.BookRankMapper;
import ltd.hxya.novel.book.service.IBookRankService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ltd.hxya.novel.book.vo.BookRankItemVo;
import ltd.hxya.novel.book.vo.BookRankVo;
import ltd.hxya.novel.common.constant.redis.RedisConstant;
import ltd.hxya.novel.common.exception.NovelGlobalException;
import ltd.hxya.novel.common.bean.ResultEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hxya
 * @since 2022-09-18
 */
@Service
public class BookRankServiceImpl extends ServiceImpl<BookRankMapper, BookRank> implements IBookRankService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 从数据库中查询小说排行的分类，并封装到BookRankVo中，
     * 将查询到的数据保存到Redis中
     * @return
     */
    @Override
    public List<BookRankVo> listBookRank() {
        //判断Redis中是否有该数据，如果有，则不在查询
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        String s = valueOperations.get(RedisConstant.BOOK_RANK_KEY);
        if (!StringUtils.isEmpty(s)){
            List<BookRankVo> bookRankVos = JSON.parseObject(s, new TypeReference<List<BookRankVo>>() {
            });
            return bookRankVos;
        }
        //从数据库中查询小说排行
        List<BookRank> bookRanks = this.baseMapper.selectList(null);
        //判断是否为空，如果为空，则不进行遍历查询
        if (CollectionUtils.isEmpty(bookRanks)){
            throw new NovelGlobalException(ResultEnum.NOT_DATA.getCode(), ResultEnum.NOT_DATA.getMsg());
        }
        Map<Long,BookRankVo> bookRankVoMap = new HashMap<>();
        List<BookRankVo> bookRankVos = bookRanks.stream().filter(bookRank -> {
            //查询出bookRank中的父类id为0的值

            if ("0".equals(bookRank.getRankParentId())) {
                return true;
            }
            return false;
        }).map(bookRank -> {
            //将bookRank封装成bookRankVo
            BookRankVo bookRankVo = new BookRankVo();
            BeanUtils.copyProperties(bookRank, bookRankVo);
            return bookRankVo;
        }).collect(Collectors.toList());
        //对每一个BookRankVo中添加bookRankItemVo
        if (CollectionUtils.isEmpty(bookRankVos)){
            throw new NovelGlobalException(ResultEnum.NOT_DATA.getCode(), ResultEnum.NOT_DATA.getMsg());
        }
        List<BookRankVo> bookRankVos1 = bookRankVos.stream().map(bookRankVo -> {
            List<BookRankItemVo> bookRankItemVos = new ArrayList<>();
            bookRanks.stream().forEach(bookRank -> {
                if (bookRank.getRankParentId()!=null&&bookRank.getRankParentId().equals(bookRankVo.getId())) {
                    //将bookRank封装成BookRankItem
                    BookRankItemVo bookRankItemVo = new BookRankItemVo();
                    BeanUtils.copyProperties(bookRank, bookRankItemVo);
                    bookRankItemVos.add(bookRankItemVo);
                }
            });
            bookRankVo.setChildRankCats(bookRankItemVos);
            return bookRankVo;
        }).collect(Collectors.toList());

        //将查询到的结果封装到Redis中
        String json = JSON.toJSONString(bookRankVos1);
        valueOperations.set(RedisConstant.BOOK_RANK_KEY,json);

        return bookRankVos1;
    }
}
